
// ***********************
// * GManage - thomasfn *
// ***********************
// Core file, loaded before everything else

// Core table
GManage = {}

// Core values
GManage.Valid = true
GManage.Version = "1.0.0"

// Debug mode
GManage.DebugMode = true

// ---------------------------------------------------------------------------------------------------------------
// Msg - Prints a GManage message to console
// ---------------------------------------------------------------------------------------------------------------
function GManage.Msg( message, dbg )
	if (GManage.DebugMode || !dbg) then
		Msg( "GManage: ", message, "\n" )
	end
end

// ---------------------------------------------------------------------------------------------------------------
// Mid - Calculates a point between two other numbers based on a decimal
// ---------------------------------------------------------------------------------------------------------------
function math.Mid( a, b, dec )
	return a+((b-a)*dec)
end

// ---------------------------------------------------------------------------------------------------------------
// If we are serverside, add clientside files for download
// ---------------------------------------------------------------------------------------------------------------
if ( SERVER ) then

	AddCSLuaFile( "gmanage/sh_core.lua" )
	AddCSLuaFile( "gmanage/plugs.lua" )

end

// ---------------------------------------------------------------------------------------------------------------
// Load the plug system
// ---------------------------------------------------------------------------------------------------------------
include( "gmanage/plugs.lua" )
require( "glon" )
require( "datastream" )
require( "plugs" )

GManage.Plugs = plugs

// ---------------------------------------------------------------------------------------------------------------
// Define some platform enums and functions
// ---------------------------------------------------------------------------------------------------------------
PLUG_PLATFORM_CLIENT = "Client"
PLUG_PLATFORM_SERVER = "Server"
PLUG_PLATFORM_SHARED = "Shared"

function IsPlatform( pl )
	if ( pl == PLUG_PLATFORM_CLIENT ) then return CLIENT end
	if ( pl == PLUG_PLATFORM_SERVER ) then return SERVER end
	if ( pl == PLUG_PLATFORM_SHARED ) then return true end
end

// ---------------------------------------------------------------------------------------------------------------
// Hook the plug system
// ---------------------------------------------------------------------------------------------------------------
plugs.Hook( PLUG_PLATFORM_SHARED, "Think" )
plugs.Hook( PLUG_PLATFORM_SHARED, "Tick" )

// ---------------------------------------------------------------------------------------------------------------
// Load all plugs
// ---------------------------------------------------------------------------------------------------------------
local PlugNames = {}
PLUG = {}
for _, v in pairs( file.FindInLua( "gmanage/plugs/*.lua" ) ) do
	local filename = "gmanage/plugs/" .. v
	PLUG = {}
	PLUG._CLASS = string.Left( v, string.len( v ) - 4 )
	PLUG._FILENAME = filename
	include( filename )
	local p = PLUG.Platform or PLUG_PLATFORM_SHARED
	if SERVER && ( ( p == PLUG_PLATFORM_CLIENT ) || ( p == PLUG_PLATFORM_SHARED ) ) then AddCSLuaFile( filename ) end
	if ( IsPlatform( p ) ) then
		plugs.Register( PLUG.ClassName, PLUG )
		table.insert( PlugNames, PLUG.ClassName )
	end
	PLUG = nil
end

// ---------------------------------------------------------------------------------------------------------------
// Read mount data
// ---------------------------------------------------------------------------------------------------------------
plugs.ReadMountData()

// ---------------------------------------------------------------------------------------------------------------
// Instance all plugs
// ---------------------------------------------------------------------------------------------------------------
for _, name in pairs( PlugNames ) do
	plugs.Instance( name )
end

// ---------------------------------------------------------------------------------------------------------------
// Call plug events
// ---------------------------------------------------------------------------------------------------------------
plugs.Call( "InitDataStorage" )
plugs.Call( "Initialize" )
plugs.Call( "Init" )
plugs.Call( "AddHooks" )
plugs.Call( "PlayerExtend", _R.Player )
plugs.Call( "EntityExtend", _R.Entity )

// ---------------------------------------------------------------------------------------------------------------
// Add con command hooks
// ---------------------------------------------------------------------------------------------------------------
if ( CLIENT ) then

	concommand.Add( "gmanage_cl", function( pl, com, args )
		com = args[1]
		if ( !com ) then return end
		table.remove( args, 1 )
		plugs.Call( "ConCommand_CL", pl, com, args )
	end )
	
	concommand.Add( "gmanage_reload_cl", function( pl, com, args )
		include( "gmanage/sh_core.lua" )
		RunConsoleCommand( "gmanage_sv", "requestupdate" )
	end )

end
if ( SERVER ) then

	concommand.Add( "gmanage_sv", function( pl, com, args )
		com = args[1]
		if ( !com ) then return end
		table.remove( args, 1 )
		plugs.Call( "ConCommand_SV", pl, com, args )
	end )
	
	concommand.Add( "gmanage_reload", function( pl, com, args )
		if (!pl:IsSuperAdmin()) then return end
		include( "gmanage/sh_core.lua" )
	end )

end

// ---------------------------------------------------------------------------------------------------------------
// Add networking hooks
// ---------------------------------------------------------------------------------------------------------------
if ( CLIENT ) then

	usermessage.Hook( "gmanage", function( um ) plugs.Call( "UserMessage", um:ReadString(), um ) end )
	datastream.Hook( "gmanage", function( handler, id, enc, dec ) plugs.Call( "DataStream", dec ) end )
	
	usermessage.Hook( "gmanage_mount", function( um )
		local name = um:ReadString()
		plugs.Mount( name )
		plugs.Call( "PlugMounted", name )
	end )
	usermessage.Hook( "gmanage_unmount", function( um )
		local name = um:ReadString()
		plugs.Unmount( name )
		plugs.Call( "PlugUnmounted", name )
	end )
	
end
if ( SERVER ) then

	datastream.Hook( "gmanage_sv", function( pl, handler, id, enc, dec ) plugs.Call( "DataStreamSV", pl, dec ) end )

end

// ---------------------------------------------------------------------------------------------------------------
// Owner promotion
// ---------------------------------------------------------------------------------------------------------------
function GMANAGE_GiveOwnership( id )
	local ply = player.GetByGID( id )
	if ( !ply || !ply:IsValid() ) then return end
	ply:SetUsergroup( "owner" )
	print( "Ownership given to " .. ply:Nick() .. "!" )
end

concommand.Add( "gmanage_setowner", function( pl, com, args )
	if (pl && pl:IsValid() && pl.UserID && (pl:UserID() != 0)) then return end
	GMANAGE_GiveOwnership( tonumber( args[1] ) )
end )